﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using AutoMapper;
using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;

namespace KernelWorkflow.Activities
{
    public partial class GetTeacherWeekSchedule : SequenceActivity
    {
        private bool crutch_first_time = true;
        public  static readonly string GetTeacherWeekScheduleAccessObject = "GetTeacherWeekSchedule";

        //in
        public static DependencyProperty CurrentSessionProperty = DependencyProperty.Register("CurrentSession", typeof(SessionDto), typeof(GetTeacherWeekSchedule));
        public static DependencyProperty CurrentTeacherGuidProperty = DependencyProperty.Register("CurrentTeacherGuid", typeof(Guid), typeof(GetTeacherWeekSchedule));
        public static DependencyProperty CurrentDayInAWeekProperty = DependencyProperty.Register("CurrentDayInAWeek", typeof(DateTime), typeof(GetTeacherWeekSchedule));
        //out
        public static DependencyProperty TeacherWeekScheduleProperty = DependencyProperty.Register("TeacherWeekSchedule", typeof(TeacherWeekFullDataScheduleDto), typeof(GetTeacherWeekSchedule));

        [Category("AUTHORIZATION_INPUT")]
        [Browsable(true)]
        public SessionDto CurrentSession
        {
            get
            {
                return ((SessionDto)(base.GetValue(GetTeacherWeekSchedule.CurrentSessionProperty)));
            }
            set
            {
                base.SetValue(GetTeacherWeekSchedule.CurrentSessionProperty, value);
            }
        }

        [Category("DATA_INPUT")]
        [Browsable(true)]
        public Guid CurrentTeacherGuid
        {
            get
            {
                return ((Guid)(base.GetValue(GetTeacherWeekSchedule.CurrentTeacherGuidProperty)));
            }
            set
            {
                base.SetValue(GetTeacherWeekSchedule.CurrentTeacherGuidProperty, value);
            }
        }        

        [Category("DATA_INPUT")]
        [Browsable(true)]
        public DateTime CurrentDayInAWeek
        {
            get
            {
                return ((DateTime)(base.GetValue(GetTeacherWeekSchedule.CurrentDayInAWeekProperty)));
            }
            set
            {
                base.SetValue(GetTeacherWeekSchedule.CurrentDayInAWeekProperty, value);
            }
        }


        [Category("DATA_OUTPUT")]
        [Browsable(true)]
        public TeacherWeekFullDataScheduleDto TeacherWeekSchedule
        {
            get
            {
                return ((TeacherWeekFullDataScheduleDto)(base.GetValue(GetTeacherWeekSchedule.TeacherWeekScheduleProperty)));
            }
            set
            {
                base.SetValue(GetTeacherWeekSchedule.TeacherWeekScheduleProperty, value);
            }
        }


        public Boolean isSessionOk = false;
        

        private void GetTeacherWeekSchedule_ExecuteCode(object sender, EventArgs e)
        {
            if (crutch_first_time)
            {
                Mapper.CreateMap<TeacherLineScheduleDto, TeacherLineWSScheduleDto>();
                this.crutch_first_time = false;
            }

            Console.WriteLine("GetTeacherWeekSchedule::GetTeacherWeekSchedule_ExectuteCode");

            AuthorizationService.AuthorizationClient authservice = new AuthorizationService.AuthorizationClient();
            isSessionOk = authservice.CheckRights(CurrentSession, GetTeacherWeekScheduleAccessObject);
            authservice.Close();
            Console.WriteLine("isSessionOk = {0}", isSessionOk);

            if (isSessionOk)
            {
                TeacherWeekSchedule = new TeacherWeekFullDataScheduleDto();
                List<TeacherDayItemWSScheduleDto> days = new List<TeacherDayItemWSScheduleDto>();
                TeacherWeekSchedule.Days = days;

                ScheduleService.SchedulingServiceClient scheduleclient = new ScheduleService.SchedulingServiceClient();
                scheduleclient.Open();

                TeacherWeekScheduleDto dockweek = scheduleclient.GetTeacherScheduleForAWeek(CurrentSession,
                    CurrentTeacherGuid, CurrentDayInAWeek);

                scheduleclient.Close();

                HrService.HrServiceClient hrclient = new HrService.HrServiceClient();
                hrclient.Open();
                DirectorateService.DirectorateServiceClient directorateclient = new DirectorateService.DirectorateServiceClient();
                directorateclient.Open();


                if (dockweek != null && dockweek.Days != null)
                {
                    IEnumerator<TeacherDayItemScheduleDto> dockdaysEnumerator = dockweek.Days.GetEnumerator();
                    TeacherWeekSchedule.RequestedWeek = dockweek.RequestedWeek;
                    TeacherWeekSchedule.Teacher = hrclient.GetTeacher(CurrentSession, dockweek.TeacherGuid);

                    while (dockdaysEnumerator.MoveNext())
                    {
                        TeacherDayItemWSScheduleDto day = new TeacherDayItemWSScheduleDto();
                        days.Add(day);
                        List<TeacherLineWSScheduleDto> lines = new List<TeacherLineWSScheduleDto>();
                        day.WeekDay = dockdaysEnumerator.Current.WeekDay;
                        day.Lines = lines;

                        if (dockdaysEnumerator.Current.Lines != null)
                        {
                            IEnumerator<TeacherLineScheduleDto> docklinesEnumerator = dockdaysEnumerator.Current.Lines.GetEnumerator();
                            while (docklinesEnumerator.MoveNext())
                            {
                                TeacherLineWSScheduleDto line = Mapper.Map<TeacherLineScheduleDto, TeacherLineWSScheduleDto>(docklinesEnumerator.Current);
                                lines.Add(line);
                                line.Subject = directorateclient.GetSubject(CurrentSession, docklinesEnumerator.Current.SubjectGuid);
                            }
                        }
                    }
                }

                hrclient.Close();
                directorateclient.Close();

                Console.WriteLine("Data constructing finished");
            }
        }


        public GetTeacherWeekSchedule()
        {
            InitializeComponent();
        }
    }
}
